package com.china08.yxyapi.api.manage.edu.office.outernotice;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter;
import com.china08.yxyapi.entity.mg.office.YxyNoticeOutter.YxyNotifictionSchool;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeOutterRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教育局行政办公 外部通知 搜索通知
 * 
 */
@CrossOrigin
@RestController("EduOuterNoticeSrhController")
public class OuterNoticeReceiveSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeOutterRepository yxyNoticeOutterRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/outerNotice/receive/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        // 当前教育局id
        String edbId = getProfile(request, response).getSchoolId();
        // 未删除状态
        int isDelete = 0;

        List<YxyNoticeOutter> page = yxyNoticeOutterRepository.findByReceiveSchoolSchoolIdAndReceiveSchoolIsDelete(edbId, isDelete, pageable);
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        for (YxyNoticeOutter yxyNoticeOutter : page) {
            RespModel respModel = new RespModel();
            respModel.id = yxyNoticeOutter.getId();
            respModel.userId = yxyNoticeOutter.getUserId();
            respModel.userNick = yxyNoticeOutter.getUserNick();
            respModel.schoolId = yxyNoticeOutter.getEdbId();
            respModel.schoolNick = yxyNoticeOutter.getEdbName() == null ? "未知教育局" : yxyNoticeOutter.getEdbName();
            respModel.title = yxyNoticeOutter.getTitle();
            respModel.createdDate = DateToolUtils.format(yxyNoticeOutter.getCreatedDate(), "yyyy-MM-dd");
            // JAVA 8 判断查询接受用户信息
            Optional<YxyNotifictionSchool> yxyNotifictionSchool = yxyNoticeOutter.getCurrentReceiver(edbId);
            respModel.isRead = yxyNotifictionSchool.get().getIsRead();

            yxyNoticeOutter.getReceiveSchool();

            respModelLst.add(respModel);
        }
        Integer totalElements = yxyNoticeOutterRepository.countByReceiveSchoolSchoolIdAndReceiveSchoolIsDelete(edbId, isDelete);
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, totalElements);
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        public String id;
        // 消息标题
        public String title;
        // 发件人id
        public String userId;
        // 发件人名称
        public String userNick;
        // 发布对象 教育局id
        public String schoolId;
        // 发布对象 教育局名称
        public String schoolNick;
        // 是否已读 0:未读 1:已读
        public int isRead;
        public String createdDate;
    }
}
